package com.gfxy.modules.excel;

import com.gfxy.modules.common.entity.MemberList;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;

import java.text.NumberFormat;
import java.util.List;

/**
 * @author: lijixin 2021/7/6 4:13 下午
 */
public class ExcelUtil {
    public static String formatCell(Cell cell){
        String cellValue;
        if(cell == null){
            cellValue = "";
        } else if(cell.getCellTypeEnum() == CellType.NUMERIC){
            if (HSSFDateUtil.isCellDateFormatted(cell)) {
                cellValue = DateFormatUtils.format(cell.getDateCellValue(), "yyyy-MM-dd");
            } else {
                NumberFormat nf = NumberFormat.getInstance();
                cellValue = String.valueOf(nf.format(cell.getNumericCellValue())).replace(",", "");
            }
        }else if(cell.getCellTypeEnum() == CellType.STRING){
            cellValue = String.valueOf(cell.getStringCellValue());
        }else if(cell.getCellTypeEnum() == CellType.BOOLEAN){
            cellValue = String.valueOf(cell.getBooleanCellValue());
        }else if(cell.getCellTypeEnum() == CellType.ERROR){
            cellValue = "错误类型";
        }else if(cell.getCellTypeEnum() == CellType.FORMULA){
            cellValue = String.valueOf(cell.getNumericCellValue());;
        } else {
            cellValue = "";
        }

        return cellValue.trim(); //有必要自行trim
    }

    public static double computeProportion(int ranked, int totalNum){

        if(totalNum==1){
            return 1.0;
        } else if(totalNum == 2){
            if(ranked ==1){
                return 0.6;
            } else if(ranked == 2){
                return 0.4;
            } else {
                return 0.0;
            }
        } else if(totalNum == 3){
            if(ranked ==1){
                return 0.5;
            } else if(ranked == 2){
                return 0.3;
            } else if(ranked == 3){
                return 0.2;
            } else {
                return 0.0;
            }
        } else if(totalNum == 4){
            if(ranked ==1){
                return 0.5;
            } else if(ranked == 2){
                return 0.3;
            } else if(ranked == 3){
                return 0.1;
            } else if(ranked == 4){
                return 0.1;
            } else {
                return 0.0;
            }
        } else if(totalNum == 5){
            if(ranked ==1){
                return 0.5;
            } else if(ranked == 2){
                return 0.2;
            } else if(ranked == 3){
                return 0.1;
            } else if(ranked == 4){
                return 0.1;
            } else if(ranked == 5){
                return 0.1;
            } else {
                return 0.0;
            }
        } else if(totalNum >=6){
            if(ranked == 1){
                return 0.5;
            } else if(ranked > 1){

                return Math.floor(0.5*10000/(totalNum-1))/10000.0 ;
            } else {
                return 0.0;
            }
        } else {
            return 0.0;
        }
    }

//    public static void computeScore(List<MemberList> memberLists, double totalScore){
//        int totalMemberNum = memberLists.size();
//        if(totalMemberNum == 1){
//            memberLists.get(0).setProportion(1.0);
//            memberLists.get(0).setScore(totalScore);
//        } else if(totalMemberNum == 2){
//            memberLists.get(1).setProportion(0.4);
//            memberLists.get(1).setScore(totalScore * 0.4);
//            memberLists.get(0).setProportion(0.6);
//            memberLists.get(0).setScore(totalScore * 0.6);
//        } else if( totalMemberNum == 3){
//            memberLists.get(2).setProportion(0.2);
//            memberLists.get(2).setScore(totalScore * 0.2);
//            memberLists.get(1).setProportion(0.3);
//            memberLists.get(1).setScore(totalScore * 0.3);
//            memberLists.get(0).setProportion(0.5);
//            memberLists.get(0).setScore(totalScore * 0.5);
//        } else if(totalMemberNum ==4){
//            memberLists.get(3).setProportion(0.1);
//            memberLists.get(3).setScore(totalScore * 0.1);
//            memberLists.get(2).setProportion(0.1);
//            memberLists.get(2).setScore(totalScore * 0.1);
//            memberLists.get(1).setProportion(0.3);
//            memberLists.get(1).setScore(totalScore * 0.3);
//            memberLists.get(0).setProportion(0.5);
//            memberLists.get(0).setScore(totalScore * 0.5);
//        } else if( totalMemberNum ==5){
//            memberLists.get(4).setProportion(0.1);
//            memberLists.get(4).setScore(totalScore * 0.1);
//            memberLists.get(3).setProportion(0.1);
//            memberLists.get(3).setScore(totalScore * 0.1);
//            memberLists.get(2).setProportion(0.1);
//            memberLists.get(2).setScore(totalScore * 0.1);
//            memberLists.get(1).setProportion(0.2);
//            memberLists.get(1).setScore(totalScore * 0.2);
//            memberLists.get(0).setProportion(0.5);
//            memberLists.get(0).setScore(totalScore * 0.5);
//        } else if(totalMemberNum >=6){
//            double totalProportios = 0.0;
//            double totalScores = 0.0;
//            double tProportio = Math.floor(0.5*10000/(totalMemberNum-1))/10000.0;
//            double tScore = Math.floor(totalScore * tProportio);
//            for (int i = 1; i < memberLists.size(); i++) {
//                totalProportios = totalProportios + tProportio;
//                totalScores = totalScores + tScore;
//
//                memberLists.get(i).setProportion(tProportio);
//                memberLists.get(i).setScore(tScore);
//            }
//            memberLists.get(0).setProportion(1 - totalProportios);
//            memberLists.get(0).setScore(totalScore - totalScores);
//        }
//    }

}
